package com.atguigu.spzx.manager.interceptor;

import com.atguigu.spzx.manager.config.LoginAuthConfig;
import jakarta.annotation.Resource;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableConfigurationProperties(value = LoginAuthConfig.class)
public class SpzxWebMvcConfigurer implements WebMvcConfigurer {
    @Resource
    LoginAuthInterceptor loginAuthInterceptor;
    @Resource
    LoginAuthConfig loginAuthConfig;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//表示前端访问哪些路径时允许跨域
                .allowCredentials(true)//是否允许携带cookie
                .allowedOriginPatterns("*")//允许哪些前端服务器跨域访问
                .allowedMethods("*")//允许哪些请求方式
                .allowedHeaders("*")//允许哪些请求头
                ;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry
                //配置拦截器
                .addInterceptor(loginAuthInterceptor)
                // 拦截器拦截的路径规则
                .addPathPatterns("/**")
                // 拦截器放行路径规则集合
                .excludePathPatterns(loginAuthConfig.getWhiteUrls());
    }
}
